package com.lmx.frameanimationdemo;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

/**
 * @author Leung
 * @date 2020/5/2 17:21
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView imgFan;
    private AnimationDrawable fanAnim;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        imgFan = (ImageView) findViewById(R.id.imgFan);
        ImageButton imgBtnOff = (ImageButton) findViewById(R.id.imgBtnOff);
        ImageButton imgBtnLevel1 = (ImageButton) findViewById(R.id.imgBtnLevel1);
        ImageButton imgBtnLevel2 = (ImageButton) findViewById(R.id.imgBtnLevel2);
        ImageButton imgBtnLevel3 = (ImageButton) findViewById(R.id.imgBtnLevel3);

        imgBtnOff.setOnClickListener(this);
        imgBtnLevel1.setOnClickListener(this);
        imgBtnLevel2.setOnClickListener(this);
        imgBtnLevel3.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imgBtnOff:
                if (fanAnim != null) {
                    fanAnim.stop();
                }
                return;
            case R.id.imgBtnLevel1:
                imgFan.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.level1_anim));
                break;
            case R.id.imgBtnLevel2:
                imgFan.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.level2_anim));
                break;
            case R.id.imgBtnLevel3:
                imgFan.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.level3_anim));
                break;
            default:
        }
        fanAnim = (AnimationDrawable) imgFan.getDrawable();
        fanAnim.start();
    }
}
